package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.mapper.*;
import com.ujiuye.pojo.*;
import com.ujiuye.service.ISetmealService;
import com.ujiuye.utils.MessageConstant;
import com.ujiuye.utils.PageResult;
import com.ujiuye.utils.QueryPageBean;
import com.ujiuye.utils.Result;
import com.ujiuye.vo.SetmealCheckgroupVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 套餐-一个套餐有多个检查组 服务实现类
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;
    @Resource
    private CheckitemMapper checkitemMapper;


    @Override
    public PageResult listPage(QueryPageBean queryPageBean) {
        //创建分页对象
        Page<Setmeal> page =
                new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        //创建条件构造器
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        //判断查询条件是否为空
        if (queryString!=null && queryString.length()>0){
            queryWrapper.eq("code",queryString)
                    .or().like("name",queryString)
                    .or().eq("helpCode",queryString);
        }
        //执行查询
        Page<Setmeal> setmealPage = setmealMapper.selectPage(page, queryWrapper);
        //创建返回结果对象
        PageResult pageResult = new PageResult(setmealPage.getTotal(),setmealPage.getRecords());
        return pageResult;
    }

    @Override
    public void save(Setmeal setmeal, Integer[] checkgroupIds) {
        //添加套餐
        setmealMapper.insert(setmeal);
        //添加中间表
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup(setmeal.getId(),checkgroupId);
            setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
    }

    @Override
    public Result getById(int id) {
        //查询套餐的信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询这个套餐有什么检查组
        QueryWrapper<SetmealCheckgroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",id);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(queryWrapper);
        /*
        setmealCheckgroups
        [{34,29},{34,30},{34,28}] ----->    [28,29,30]
         */
        List<Integer> checkgroupIds = new ArrayList<>();
        for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroups) {
            checkgroupIds.add(setmealCheckgroup.getCheckgroupId());
        }
        //把setmeal和checkgroupIds组装在一起
        SetmealCheckgroupVo setmealCheckgroupVo = new SetmealCheckgroupVo(setmeal,checkgroupIds);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmealCheckgroupVo);
    }

    @Override
    public Setmeal getBySetmealId(int id) {
        //通过id查询套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询这个套餐有什么检查组----中间表
        QueryWrapper<SetmealCheckgroup> setmealCheckgroupQueryWrapper = new QueryWrapper<>();
        setmealCheckgroupQueryWrapper.eq("setmeal_id",id);
        List<SetmealCheckgroup> setmealCheckgroupList = setmealCheckgroupMapper.selectList(setmealCheckgroupQueryWrapper);
        List<Checkgroup> checkgroupList = new ArrayList<>();//用于保存checkgroup
        if (setmealCheckgroupList!=null && setmealCheckgroupList.size()>0){
            for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroupList) {
                //通过Checkgroupid查询检查组的信息
                Checkgroup checkgroup = checkgroupMapper.getById(setmealCheckgroup.getCheckgroupId());
                //查询这个检查组有什么检查项
                QueryWrapper<CheckgroupCheckitem> checkgroupCheckitemQueryWrapper = new QueryWrapper<>();
                checkgroupCheckitemQueryWrapper.eq("checkgroup_id",checkgroup.getId());
                List<CheckgroupCheckitem> checkgroupCheckitemList = checkgroupCheckitemMapper.selectList(checkgroupCheckitemQueryWrapper);
                List<Checkitem> checkitemList = new ArrayList<>();//用于保存checkitem
                if (checkgroupCheckitemList!=null && checkgroupCheckitemList.size()>0){
                    for (CheckgroupCheckitem checkgroupCheckitem : checkgroupCheckitemList) {
                        //通过Checkitemid查询检查项的信息
                        Checkitem checkitem = checkitemMapper.getById(checkgroupCheckitem.getCheckitemId());
                        //把检查项存入集合
                        checkitemList.add(checkitem);
                    }
                    //把检查项集合存入检查组对象中
                    checkgroup.setCheckitemList(checkitemList);
                }
                //把检查组存入集合
                checkgroupList.add(checkgroup);
            }
            setmeal.setCheckgroupList(checkgroupList);
        }
        return setmeal;
    }


    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        //更新套餐，添加成功后主键会自动返回setmeal的id属性
        setmealMapper.updateById(setmeal);
        //修改检查组，把原来的删除(中间表记录)再重新添加
        QueryWrapper<SetmealCheckgroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",setmeal.getId());
        setmealCheckgroupMapper.delete(queryWrapper);
        //重新添加中间表
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
    }

}
